package cn.dnui_lxx432.dnuifood_16110100432.controller;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.widget.Toast;

import java.util.List;

import cn.dnui_lxx432.dnuifood_16110100432.Adapter.FoodListAdapter;
import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.FoodBean;
import cn.dnui_lxx432.dnuifood_16110100432.R;
import cn.dnui_lxx432.dnuifood_16110100432.listener.OnRetrofitListener;
import cn.dnui_lxx432.dnuifood_16110100432.model.FoodModel;
import cn.dnui_lxx432.dnuifood_16110100432.model.ShopModel;

public class SearchResultActivity extends AppCompatActivity implements OnRetrofitListener<List<FoodBean>> {
    private RecyclerView recyclerView;
    private String search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result);

        initViews();
        initData();

    }

    void initViews(){
        recyclerView = (RecyclerView)findViewById(R.id.search_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    void initData(){

        search = getIntent().getStringExtra("search");
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodBySearch(search,this);
    }



    @Override
    public void onSuccess(List<FoodBean> foodBeans, int flag) {
        if (foodBeans!=null&&foodBeans.size()>0){
            FoodListAdapter adapter = new FoodListAdapter(SearchResultActivity.this,foodBeans);
            recyclerView.setAdapter(adapter);
        }else {
            Toast.makeText(SearchResultActivity.this,"没有搜索结果",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(SearchResultActivity.this,"搜索网络错误",Toast.LENGTH_SHORT).show();
    }
}